/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.osgg.managers;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.NonUniqueResultException;
import net.osgg.daos.ClienteDAO;
import net.osgg.daos.ClienteDAOImpl;
import net.osgg.entities.Cliente;
import net.osgg.util.HibernateUtil;
import org.hibernate.HibernateException;

/**
 *
 * @author omar
 */
public class ClienteMgrImpl implements ClienteMgr {
 
    private ClienteDAO clienteDAO = new ClienteDAOImpl();
 
    public List<Cliente> buscarClientePorNombre(String nombre) {
        List<Cliente> clientes = null;
        try {
            HibernateUtil.beginTransaction();
            clientes = clienteDAO.buscarPorNombre(nombre);
            HibernateUtil.commitTransaction();
        } catch (NonUniqueResultException ex) {
            System.out.println("Handle your error here");
            System.out.println("Query returned more than one results.");
        } catch (HibernateException ex) {
            ex.printStackTrace();
        }
        return clientes;
    }
 
    public List<Cliente> cargarTodosLosClientes() {
        List<Cliente> clientes = new ArrayList<Cliente>();
        try {
            HibernateUtil.beginTransaction();
            clientes = clienteDAO.devolverTodos(Cliente.class);
            HibernateUtil.commitTransaction();
        } catch (HibernateException ex) {
            ex.printStackTrace();
        }
        return clientes;
    }
 
    public void guardarClienteNuevo(Cliente cliente) {
        try {
            HibernateUtil.beginTransaction();
            clienteDAO.nuevo(cliente);
            HibernateUtil.commitTransaction();
        } catch (HibernateException ex) {
            ex.printStackTrace();
            HibernateUtil.rollbackTransaction();
        }
    }
 
    public void actualizarCliente(Cliente cliente){
        try {
            HibernateUtil.beginTransaction();
            clienteDAO.actualizar(cliente);
            HibernateUtil.commitTransaction();
        } catch (HibernateException ex) {
            ex.printStackTrace();
            HibernateUtil.rollbackTransaction();
        }        
    }
    
    public Cliente buscarClientePorID(Integer id) {
        Cliente cliente = null;
        try {
            HibernateUtil.beginTransaction();
            cliente = (Cliente) clienteDAO.buscarPorID(Cliente.class, id);
            HibernateUtil.commitTransaction();
        } catch (HibernateException ex) {
            ex.printStackTrace();
        }
        return cliente;
    }
 
    public void borrarCliente(Cliente cliente) {
        try {
            HibernateUtil.beginTransaction();
            clienteDAO.borrar(cliente);
            HibernateUtil.commitTransaction();
        } catch (HibernateException ex) {
            ex.printStackTrace();
            HibernateUtil.rollbackTransaction();
        }
    }
}

